package com.cynen.job.task;

import com.cynen.job.pojo.JobInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

@Component
public class JobProcessor implements PageProcessor {
    private  String initUrl = "https://search.51job.com/list/000000,000000,0000,32%252C01,9,99,java,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
    @Override
    public void process(Page page) {
        // 判断当前获取的page是列表页面还是详情页面.
        // 判断方法1: 判断page中是否包含职位列表信息?
        // 判断方法2: 判断当前页面是否是html详情页[暂不可取]
        List<Selectable> selectableList = page.getHtml().css("div#resultList div.el").nodes();
        if (selectableList.size() > 0) {
            // 如果是列表页面,就将url添加到schedule中...
            for (Selectable selectable : selectableList) {
                String jobInfourl = selectable.links().toString();
                // System.out.println(jobInfourl);
                page.addTargetRequest(jobInfourl);
            }
            // 获得翻页的url ,每页有2个,一个上一页,一个下一页.
            List<Selectable> nodes = page.getHtml().css("div.p_in ul li.bk").nodes();
            // 只取下一页.
            page.addTargetRequest(nodes.get(1).links().toString());
        } else {
            // 如果是详情页面,就获取数据
            this.save(page);
        }
    }

    // 详情页面,保存数据.
    private void save(Page page) {
        // 将所有的属性值,保存在bean中.
        JobInfo jobInfo = new JobInfo();

        String msg = page.getHtml().css("div.cn p.msg","title").toString();


        // 公司名称
        jobInfo.setCompanyName(page.getHtml().css("div.cn p.cname a","text").toString());
        jobInfo.setCompanyAddr(msg.substring(0,msg.indexOf("|")-2));
        jobInfo.setCompanyInfo(page.getHtml().css("div.tmsg","text").toString());

        jobInfo.setJobName(page.getHtml().css("div.cn h1","text").toString());
        // jobInfo.setJobAddr(page.getHtml().css("div.bmsg p.fp").nodes().get(1).css("p","text").toString());
        jobInfo.setJobAddr(page.getHtml().xpath("/html/body/div[3]/div[2]/div[3]/div[2]/div/p/text()").toString());

        List<String> jobds = page.getHtml().css("div.job_msg p", "text").all();
        String jobmsg ="";
        for (int i = 0; i < jobds.size(); i++) {
            jobmsg = jobmsg.concat(jobds.get(i));
        }
        jobInfo.setJobInfo(jobmsg);
        jobInfo.setJobUrl(page.getUrl().toString());

        String salary = page.getHtml().css("div.cn strong","text").toString();
        int[] sa = SalaryUtils.getSalary(salary);

        jobInfo.setSalaryMin(sa[0]);
        jobInfo.setSalaryMax(sa[1]);
        // 更新日期
        jobInfo.setPubdate(msg.substring(msg.indexOf("发布")-5,msg.indexOf("发布")));
        // 将bean 存放到resultItems中.
        page.putField("jobinfo",jobInfo);
    }

    private Site site = Site.me()
            .setCharset("gbk") // 设置编码
            .setTimeOut(10*1000) // 设置超时时间
            .setRetryTimes(2) // 设置重试次数.
            .setRetrySleepTime(2000); // 设置重试间隔时间

    @Override
    public Site getSite() {
        return site;
    }


    @Autowired
    private JobPipline jobPipline;
    /**
     * 定时任务.
     */
    // initialDelay 应用启动后,隔多久,开始任务.
    // fixedDelay 每隔多久执行一次任务.
    @Scheduled(initialDelay = 1000,fixedDelay = 100*1000)
    public void job(){
        Spider.create(new JobProcessor())
                .addUrl(initUrl) // 初始的url
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(1000000)))
                .addPipeline(jobPipline)
                .thread(5)
                .run();
    }
}
